﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.VirtualAccounts;


namespace Application.MainModule.VirtualAccountsManagement
{
    public class VirtualAccountManagementService : IVirtualAccountManagementService, IDisposable
    {
        #region Members

        IVirtualAccountRepository _VirtualAccountRepository;

        #endregion

        #region Constructor

        public VirtualAccountManagementService(IVirtualAccountRepository bankAccountRepository)
        {
            if (bankAccountRepository == (IVirtualAccountRepository)null)
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);

            _VirtualAccountRepository = bankAccountRepository;
        }

        #endregion


        public void AddVirtualAccount(VirtualAccount virtualAccount)
        {
            IUnitOfWork unitOfWork = _VirtualAccountRepository.UnitOfWork as IUnitOfWork;
            _VirtualAccountRepository.Add(virtualAccount);
            unitOfWork.Commit();
        }

        public void ChangeVirtualAccount(VirtualAccount virtualAccount)
        {
            IUnitOfWork unitOfWork = _VirtualAccountRepository.UnitOfWork as IUnitOfWork;
            _VirtualAccountRepository.Modify(virtualAccount);
            unitOfWork.CommitAndRefreshChanges();
        }

        public void RemoveVirtualAccount(VirtualAccount virtualAccount)
        {
            if (virtualAccount == (VirtualAccount)null)
                throw new ArgumentNullException("virtualAccount");

            IUnitOfWork unitOfWork = _VirtualAccountRepository.UnitOfWork as IUnitOfWork;
            virtualAccount.IsEnabled = false;
            _VirtualAccountRepository.Modify(virtualAccount);
            unitOfWork.CommitAndRefreshChanges();
        }


        public List<VirtualAccount> FindVirtualAccounts(Guid userId)
        {
            VirtualAccountUserIdSpecification spec = new VirtualAccountUserIdSpecification(userId);
            return _VirtualAccountRepository.FindVirtualAccounts(spec).ToList<VirtualAccount>();
        }

        #region IDisposable

        /// <summary>
        /// Dispose associated context with this domain service
        /// </summary>
        public void Dispose()
        {
            if (_VirtualAccountRepository != null
                &&
                _VirtualAccountRepository.UnitOfWork != null)
            {
                _VirtualAccountRepository.UnitOfWork.Dispose();
            }
        }

        #endregion

        public VirtualAccount FindVirtualAccountById(Guid virtualAccountId)
        {
            VirtualAccountVirtualAccountIdSpecification spec = new VirtualAccountVirtualAccountIdSpecification(virtualAccountId);
            return _VirtualAccountRepository.FindVirtualAccount(spec);
        }
    }
}
